/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package src;

import java.util.Hashtable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import src.commands.*;
import src.exceptions.eNoPermission;

/**
 *
 * @author pabloavila
 */
public class Factory {

    /* Declaramos una tabla hash que usaremos para almacenar ops
     * La clave: op + "_" + Permisos
     * Valor: Comando a generar
     */
    private Hashtable hs = new Hashtable();

    public Factory(HttpServletRequest request, HttpServletResponse response){
        // Aqui creamos los posibles comandos
        synchronized(hs){
            String key;
            // Nothing
            key = "_" + Permisos.ADMIN.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoNothing(request,response));
            key = "_" + Permisos.USER.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoNothing(request,response));
            key = "_" + Permisos.GUEST.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoNothing(request,response));
            // Logout
            key = "Logout_"+Permisos.ADMIN.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoLogout(request,response));
            key = "Logout_"+Permisos.USER.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoLogout(request,response));
            key = "Logout_"+Permisos.GUEST.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoLogout(request,response));

            // Add User: AddUser_Administrador
            key = "AddUser_"+Permisos.ADMIN.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoAddUser(request,response));
            // Edit User
            key = "EditUser_"+Permisos.ADMIN.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoEditUser(request,response));
            // Remove User
            key = "RemoveUser_"+Permisos.ADMIN.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoRemoveUser(request,response));
            // Post Message
            key = "Post_"+Permisos.ADMIN.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoPost(request,response));
            key = "Post_"+Permisos.USER.toString();
            if (!hs.containsKey(key))
                hs.put(key, new DoPost(request,response));
            // Edit Message
            //key = "EditMsg_"+Permisos.ADMIN.toString();
            //if (!hs.containsKey(key))
              //  hs.put(key, new DoEditMsg(request,response));
            // Remove Message
            //key = "RemoveMsg_"+Permisos.ADMIN.toString();
            //if (!hs.containsKey(key))
              //  hs.put(key, new DoRemoveMsg(request,response));
        }
    }

    // GetComando
    public Command getComando(String frase){
        try {
            // Cogemos la tabla y miramos la frase
            Command c = (Command) hs.get(frase);
            if (c == null) {
                throw new eNoPermission("El usuario no tiene permisos");
            } else {
                return c;
            }
        } catch (eNoPermission ex) {
            // Coger la excepcion
            return null;
        }
    }

}
